//
//  CarSourceType.m
//  CarTown
//
//  Created by xionghao on 15/9/5.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "CarSourceType.h"
#import "AppDelegate.h"

@implementation CarSourceType

@dynamic csid;
@dynamic name;
+(void)addCarSourceType:(NSArray *)data{
    AppDelegate *delegate=[[UIApplication sharedApplication]delegate];
    NSFetchRequest *request=[NSFetchRequest fetchRequestWithEntityName:@"CarSourceType"];
    NSArray *sets= [delegate.managedObjectContext executeFetchRequest:request error:nil];
    if (sets.count>0) {
        return;
    }
    for (NSDictionary *dict in data) {
        CarSourceType *car=[NSEntityDescription insertNewObjectForEntityForName:@"CarSourceType" inManagedObjectContext:delegate.managedObjectContext];
        [car setValue:dict[@"name"] forKey:@"name"];
        [car setValue:dict[@"id"] forKey:@"csid"];
        [delegate saveContext];
    }
    
    
   }
+(NSArray *)getCarSourceTypeNotHasZhongGUI{
    AppDelegate *delegate=[[UIApplication sharedApplication]delegate];
    NSFetchRequest *request=[NSFetchRequest fetchRequestWithEntityName:@"CarSourceType"];
     NSPredicate *pre=[NSPredicate predicateWithFormat:@"name CONTAINS '中规'"];
    request.predicate=pre;
    NSArray *sets= [delegate.managedObjectContext executeFetchRequest:request error:nil];
    return sets;
}
+(instancetype)getcarsoucebyID:(NSString *)csid{
    AppDelegate *delegate=[[UIApplication sharedApplication]delegate];
    NSFetchRequest *request=[NSFetchRequest fetchRequestWithEntityName:@"CarSourceType"];
    NSPredicate *pre=[NSPredicate predicateWithFormat:@"csid=%@",csid];
    request.predicate=pre;
    NSArray *sets= [delegate.managedObjectContext executeFetchRequest:request error:nil];
    if (sets.count>0) {
        return  sets[0];
    }else{
        return nil;
    }

}
+(NSArray *)getCarSourceType{
    AppDelegate *delegate=[[UIApplication sharedApplication]delegate];
    NSFetchRequest *request=[NSFetchRequest fetchRequestWithEntityName:@"CarSourceType"];
    NSArray *sets= [delegate.managedObjectContext executeFetchRequest:request error:nil];
    int index=0;
    for (int i=0;i<sets.count ;i++) {
        CarSourceType *type=sets[i];
        if ([type.name isEqualToString:@"中规"]) {
            index=i;
        }
    }
    NSMutableArray *msets=[NSMutableArray arrayWithArray:sets];
    [msets moveObjectFromIndex:index toIndex:0];
    return msets;
}
+(instancetype)getcarsoucebyText:(NSString *)name{
    AppDelegate *delegate=[[UIApplication sharedApplication]delegate];
    NSFetchRequest *request=[NSFetchRequest fetchRequestWithEntityName:@"CarSourceType"];
    NSPredicate *pre=[NSPredicate predicateWithFormat:@"name=%@",name];
    request.predicate=pre;
    NSArray *sets= [delegate.managedObjectContext executeFetchRequest:request error:nil];
    if (sets.count>0) {
        return  sets[0];
    }else{
        return nil;
    }
}
@end
